/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vfi.bc;

import edu.upn.sigemfi.plt.be.Cuenta;
import edu.upn.sigemfi.vfi.be.DetalleTransaccion;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Programador
 */
public class DetalleTransaccionDelegate {


    private DetalleTransaccionFacadeLocal local;

    public DetalleTransaccionDelegate() {
        local = lookupDetalleTransaccionFacade();
    }

    public void crear(DetalleTransaccion detalleTransaccion) throws Exception {
        try {
            local.create(detalleTransaccion);
        } catch (Exception e) {
            local = lookupDetalleTransaccionFacade();
            throw new Exception("Crear DetalleTransaccion. " + e.getMessage(), e);
        }
    }

    public void editar(DetalleTransaccion detalleTransaccion) throws Exception {
        try {
            local.edit(detalleTransaccion);
        } catch (Exception e) {
            local = lookupDetalleTransaccionFacade();
            throw new Exception("Editar DetalleTransaccion. " + e.getMessage(), e);
        }
    }

    public void eliminar(DetalleTransaccion detalleTransaccion) throws Exception {
        try {
            local.remove(detalleTransaccion);
        } catch (Exception e) {
            local = lookupDetalleTransaccionFacade();
            throw new Exception("Eliminar DetalleTransaccion. " + e.getMessage(), e);
        }
    }

    public DetalleTransaccion buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupDetalleTransaccionFacade();
            throw new Exception("Buscar DetalleTransaccion. " + e.getMessage(), e);
        }
    }

    public List<DetalleTransaccion> BuscarMovimientosPorCuenta(Cuenta fidPltCuenta) throws Exception {
        try {
            return local.BuscarMovimientosPorCuenta(fidPltCuenta);
        } catch (Exception e) {
            local = lookupDetalleTransaccionFacade();
            throw new Exception("Listar DetalleTransaccion. " + e.getMessage(), e);
        }
    }



    public List<DetalleTransaccion> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupDetalleTransaccionFacade();
            throw new Exception("Listar DetalleTransaccion. " + e.getMessage(), e);
        }
    }


    private DetalleTransaccionFacadeLocal lookupDetalleTransaccionFacade() {
        try {
            Context c = new InitialContext();
            return (DetalleTransaccionFacadeLocal) c.lookup("java:comp/env/DetalleTransaccionFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
